from django.db import models
"""
    业务分析：
    我们的用户表中只有2种类型的用户，一种为VIP用户，一种为月租年租用户。
    vip用户：为领导车辆或者内部特权车辆，活动期间临时vip车辆
    月租年租用户：为按起止时间享有VIP权
    临时车辆：无需记录我们的用户表。临时车辆的业务流程与本系统无交集。
    特殊车辆：特殊车辆免费，只有流水，无用户记录。
"""
class Matching(models.Model):
    car_number = models.CharField(u'识别车牌', max_length=20, null=False, blank=False)
    adaptation = models.TextField(u'匹配车牌', max_length=20, null=True, blank=True)

    def __str__(self):
        return '识别结果 %s -- 匹配 %s' % (self.car_number, self.adaptation)
    class Meta:
        verbose_name = '车牌匹配'
        verbose_name_plural = "车牌匹配表"

class UserProfile(models.Model):
    """
    用户 Model
    """
    user = models.OneToOneField('auth.User', related_name='userprofile')
    name = models.CharField(u'姓名', max_length=20, null=True, blank=True)
    isadmin = models.BooleanField(u'是否是管理员', default=True)  # 用角色来区分用户类型，是管理员还是普通会员
    role = models.ManyToManyField('Role', null=True, blank=True)
    car_number = models.CharField(u'车牌号', unique=True, max_length=15, null=True, blank=True)
    vip = models.BooleanField(u'特殊车位用户', default=False)
    phone = models.CharField(u'手机号', max_length=20, null=True, blank=True)
    address = models.CharField(u'地址', max_length=100, null=True, blank=True)
    expire_at = models.DateTimeField(u'到期时间', null=True, blank=True)
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)
    def __str__(self):
        return '%s-%s' % (self.name, self.car_number)
    class Meta:
        verbose_name = '用户表'
        verbose_name_plural = "用户表"

class UserPaymentLog(models.Model):
    """
    用户缴费流水
    """
    user = models.ForeignKey('userprofile.Userprofile')
    payment_amount = models.FloatField(u'支付金额', default=0.0)
    start_time = models.DateTimeField(u'有效期开始时间')  # 使用有效期区分月租和年租
    end_time = models.DateTimeField(u'有效期结束时间')
    payment_at = models.DateTimeField(u'付款时间', blank=True, auto_now_add=True)
    remark = models.TextField(u'备注', null=True, blank=True)
    def __str__(self):
        return '%s-%s-%s-%s' % (self.user.name, self.user.car_number, self.payment_amount, self.payment_at)

    class Meta:
        verbose_name = '用户缴费流水表'
        verbose_name_plural = "用户缴费流水表"

class Role(models.Model):
    name = models.CharField(u'角色名称', max_length=100, null=False, blank=False)
    description = models.TextField(u'描述', max_length=500, null=True, blank=True)
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)
    permissions = models.ManyToManyField('Menu', through='RoleMenu', verbose_name='角色权限')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '角色表'
        verbose_name_plural = "角色表"

class Menu(models.Model):
    name = models.CharField(u'菜单名称', max_length=100, null=False, blank=False)
    parent = models.ForeignKey('self', verbose_name=u'父菜单', blank=True, null=True)
    url = models.CharField(u'菜单URL', max_length=100, null=True, blank=True)
    get = models.CharField(u'查看权限', max_length=10, default='0')
    post = models.CharField(u'修改权限', max_length=10, default='0')
    put = models.CharField(u'新增权限', max_length=10, default='0')
    delete = models.CharField(u'删除权限', max_length=10, default='0')
    icon = models.CharField(u'图标', max_length=50, null=True, blank=True)
    sort = models.PositiveIntegerField(u'排序', blank=True, null=True)
    description = models.TextField(u'描述', max_length=500, null=True, blank=True)
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)
    def __str__(self):
        return '%s:%s' % (self.id, self.name)
    class Meta:
        ordering = ('sort', 'id',)
        verbose_name = '权限菜单表'
        verbose_name_plural = "权限菜单表"
class RoleMenu(models.Model):
    role = models.ForeignKey(u'Role')
    menu = models.ForeignKey(u'Menu')
    get = models.CharField(u'查看权限', max_length=10, default='0')
    post = models.CharField(u'修改权限', max_length=10, default='0')
    put = models.CharField(u'新增权限', max_length=10, default='0')
    delete = models.CharField(u'删除权限', max_length=10, default='0')
    description = models.TextField(u'描述', max_length=500, null=True, blank=True)
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)
    def __str__(self):
        return '%s,%s,%s' % (self.id, self.role_id, self.menu_id)
    class Meta:
        verbose_name = '角色权限表'
        verbose_name_plural = "角色权限表"
class UserMenu(models.Model):
    user = models.ForeignKey(u'auth.User', null=True, blank=True)
    menu = models.ForeignKey(u'Menu', null=True, blank=True)
    get = models.CharField(u'查看权限', max_length=10, default='0')
    post = models.CharField(u'修改权限', max_length=10, default='0')
    put = models.CharField(u'新增权限', max_length=10, default='0')
    delete = models.CharField(u'删除权限', max_length=10, default='0')
    description = models.TextField(u'描述', max_length=500, null=True, blank=True)
    create_at = models.DateTimeField(blank=True, auto_now_add=True, null=True)
    update_at = models.DateTimeField(blank=True, auto_now=True, null=True)
    def __str__(self):
        return '%s,%s,%s' % (self.id, self.user_id, self.menu_id)
    class Meta:
        verbose_name = '用户权限表'
        verbose_name_plural = "用户权限表"